<?php
namespace app\api\controller;
use \think\Controller;
//use think\Request;
use think\Exception;
use think\facade\Request;
use think\facade\Cache;
use app\common\model\User;

class Index extends Controller
{

    //protected $middleware = ['Check'];  //使用Check中间件
    protected $middleware = [
        //'Check' 	=> ['except' 	=> ['num'] ],
        //'Hello' => ['only' 		=> ['hello'] ],
    ];

    /**
     * @var \think\Request Request实例
     */
    //protected $request;

    /**
     * 构造方法
     * @param Request $request Request对象
     * @access public
     */
    /*public function __construct(Request $request)
    {
        $this->request = $request;
    }*/

    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    /**
     * 使用url访问不传参
     * @return mixed
     */
    public function num()
    {
        //操作session
        $session = app('session');
        $session->set('name','xxc');
        $name = $session->get('name');

        //操作config
        $config = app('config');
        $config->set('age',18);
        $age = $config->get('age');

        //var_dump($this->request->param());
        //var_dump(\Request::param());die();
        /*Cache::set('name','value');
        echo Cache::get('name');*/

        \Cache::set('name','value');
        echo \Cache::get('name');

        /*$test = new \app\common\Test;
        echo $test->hello('thinkphp'); // 输出 hello，thinkphp*/

        echo \app\facade\Test::hello('thinkphp');

        var_dump(config('age'));die();
        var_dump(ENV());die();
        //request参数
        //$this->request->param();
        var_dump($this->request->param());
        //die();
        // return 'hello,num:' . $num;
        $all = [
            'num'=>3,
            'hh' => 1,
            'ee' => 2,
        ];
        return $this->fetch('hello',$all);
    }

    public function num1(){
        echo "<pre>";
        echo "当前函数名:".__FUNCTION__.PHP_EOL;
        echo "当前方法名:".__METHOD__.PHP_EOL;
        var_dump(\Request::param());die();
    }


    public function num2(){
        echo "<pre>";
        echo "当前函数名:".__FUNCTION__.PHP_EOL;
        echo "当前方法名:".__METHOD__.PHP_EOL;
        var_dump(\Request::param());die();
    }

    public function num3(){
        echo "<pre>";
        echo "当前函数名:".__FUNCTION__.PHP_EOL;
        echo "当前方法名:".__METHOD__.PHP_EOL;
        var_dump(\Request::param());die();
    }

    public function num4(){
        echo "<pre>";
        echo "当前函数名:".__FUNCTION__.PHP_EOL;
        echo "当前方法名:".__METHOD__.PHP_EOL;
        var_dump(\Request::param());die();
    }

    /**
     * 对请求参数进行处理
     */
    public function requestFun(){
        /*$url = url('index/blog/read', ['id' => 5, 'name' => 'thinkphp'], false);
        var_dump($url);*/
        /*var_dump(Request::param(false));
        var_dump(input('?get.id'));
        var_dump(input('param.'));
        var_dump(input('get.id/d'));*/

        /*$data = [
            'user_id'   => 113,
            'user_name' => 'think'
        ];

        $rule = [
            'type' => 'mod', // 分表方式
            'num'  => 10     // 分表数量
        ];


         $sql = \Db::name('log')
            ->fetchSql(true)
            ->partition(['user_id' => 110], "user_id", $rule)
            ->insert($data);
        // 用于查询
         $data = \Db::name('log')
            ->partition(['user_id' => 113], "user_id", $rule)
            ->where(['user_id' => 113])
            ->select();*/


        /*$sql = \Db::view(['think_user' => 'member'], ['id' => 'uid', 'name' => 'account'])
            ->view('Profile', 'truename,phone,email', 'Profile.user_id=member.id')
            ->view('Score', 'score', 'Score.user_id=Profile.id')
            ->where('score', '>', 80)
            ->fetchSql()
            ->select();
        var_dump($sql);die();*/


/*$user['user_name'] = 'thinkphp';
$user['address'] = [
    'email'    => 'thinkphp@qq.com',
    'nickname' => '流年',
];
        $sql = \Db::name('user')
    ->fetchSql()
    ->json(['address'])
    ->insert($user);

        var_dump($sql);die();*/

        //单个JSON数据更新

        /*$data['address->nickname'] = 'ThinkPHP';
        $sql = \Db::name('user')
        ->json(['address'])
        ->where('id',2)
        ->update($data);
        var_dump($sql);die();

        $sql = \Db::name('user')
            ->fetchSql()
            ->json(['address'])
            ->where('address->nickname','ThinkPHP')
            ->find();

        dump($sql); die();

        $sql = \Db::table('think_user1')
            ->fetchSql()
            ->where('user_name', ['like', '%thinkphp%'], ['like', '%kancloud%'], 'or')
            ->where('id', ['>', 0], ['<>', 10], 'and')
            ->find();
        var_dump($sql);die();

        $sql = \Db::table('think_user')
            ->fetchSql()
            ->where([
                ['user_name', 'like', 'thinkphp%'],
                ['id', 'exp', \Db::raw('>10 and nick_name like nickname% and phone is null')],
            ])
            ->select();
        var_dump($sql);die();
        // 查询单条
        try{
            \Db::name('log_0')
            ->where('id', 1)
            ->findOrFail();
        }catch (Exception $e){
            return 0;
        }*/


        /*var_dump($this->request->param());
        var_dump($this->request->get('id'));*/
        //var_dump(Request::get());

        //$all = User::select();
        /*$data = \Request::only(['name','email']);
        var_dump($data);die();*/

        //$user = User::where('id','>',1)->column('user_name','id');
        //var_dump(User::getlastsql());die();
        //$user->delete();

        /*$user = new User;
        $return = $user->save([
            'user_name'  =>  'thinkphp',
            'nick_name' =>  'thinkphp@qq.com',
            'info' => [
                'email'    => 'thinkphp@qq.com',
                'nickname'=> '流年',
            ],
        ]);*/
        /*$user = new User();
        $return = $user->save([
            'user_name'  =>  'thinkphp',
            'nick_name' =>  'thinkphp@qq.com',
            'info' => [
                'email'    => 'thinkphp@qq.com',
                'nickname'=> '流年1',
            ],
            'status' => 0,
        ]);*/
        //$user = new User();
        /*$return = User::create([
            'user_name'  =>  'thinkphp',
            'nick_name' =>  'thinkphp@qq.com',
            'info' => [
                'email'    => 'thinkphp@qq.com',
                'nickname'=> '流年1',
            ],
            'status' => 0,
        ]);

        if (!$return->getNumRows()){
            var_dump("操作失败");die();
        }*/
        \Db::listen(function ($sql, $time, $explain, $master) {
            // 记录SQL
            echo $sql . ' [' . $time . 's] ' . ($master ? 'master' : 'slave');
            // 查看性能分析结果
            dump($explain);
        });

        /*$users = User::with('profile')->select();
        //var_dump($users);die();
        foreach ($users as $user) {
            if (isset($user->profile->money)){
                echo $user->profile->money;
            }
        }*/

        /*$users = User::withJoin('profile', 'LEFT')->where('profile.money','>',100)->select();
        //var_dump($users);die();
        foreach ($users as $user) {
            if (isset($user->profile->money)){
                echo $user->profile->money;
            }
        }*/
        Cache::set('name1','xxc',3600);
        $name = Cache::get('name');
        var_dump($name);
        die();
        // 查询
        $blog = User::get(1,'profile')->toArray();
        var_dump($blog);
        // 删除当前及关联模型
        //$blog->together('content')->delete();
        //$user = User::find(1);

        //$user = User::where('id',13)->hidden(['create_time','update_time'])->append(['status_text'])->find()->toArray();
        //$user = User::where('id','=',13)->find();
        // 软删除
        //$return = $user->delete();
        //var_dump(User::getlastsql());
        //echo $user->id;
        /*$user = User::where('info->nickname','流年')->find();
        var_dump($user);die();
        var_dump(User::getlastsql());*/

        /*// 模型查询返回数据集对象
        $list = User::where('id', '>', 0)->select();
        // 对输出字段进行处理
        $list->hidden(['phone'])
            ->append(['status_text'])
            ->withAttr('nick_name', function($value, $data) {
                return strtolower($value);
            });
        dump($list);*/

    }

}
